/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.thu.laud.objectstore.fieldmanager;

import java.util.HashMap;
import java.util.Map;

import org.datanucleus.ObjectManager;
import cn.edu.thu.laud.objectstore.index.BatchMutation;

/**
 * Internalises all pending operations for a given Execution context.
 * 
 * @author zhuoan
 * 
 */
public class ObjectMutationManager {

	private Map<ObjectManager, ObjectManagerWrite> contextMutations = new HashMap<ObjectManager, ObjectManagerWrite>();
	private Map<ObjectManager, ObjectManagerDelete> contextDeletions = new HashMap<ObjectManager, ObjectManagerDelete>();

	public ObjectMutationManager() {
		
	}

	public ObjectManagerDelete beginDelete(ObjectManager context) {
		ObjectManagerDelete deleteContext = getDeletions(context);
		deleteContext.pushInstance();
		return deleteContext;

	}

	public ObjectManagerWrite beginWrite(ObjectManager context) {
		ObjectManagerWrite mutationContext = getWrites(context);
		mutationContext.pushInstance();
		return mutationContext;
	}

	/**
	 * Returns true if this is the last object to end writing. This mean the
	 * mutation for this context should be saved
	 * 
	 * @param context
	 * @param sm
	 * @return
	 * @throws Exception
	 */
	public void endDelete(ObjectManager context)
			throws Exception {
		// not our root instance, don't create a batch mutation
		if (!getDeletions(context).popInstance()) {
			return;
		}

		// it is our root instance, create the batch mutation.

		getDeletions(context).execute();
		contextDeletions.remove(context);

	}

	/**
	 * Returns true if this is the last object to end writing. This mean the
	 * mutation for this context should be saved
	 * 
	 * @param context
	 * @param sm
	 * @return
	 * @throws Exception
	 */
	public void endWrite(ObjectManager context)
			throws Exception {
		// not our root instance, don't create a batch mutation
		if (!getWrites(context).popInstance()) {
			return;
		}

		// it is our root instance, create the batch mutation.

		getWrites(context).execute();
		contextMutations.remove(context);

	}

	/**
	 * Get the mutations for this execution context
	 * 
	 * @param context
	 * @return
	 */
	private ObjectManagerWrite getWrites(ObjectManager context) {
		ObjectManagerWrite operations = contextMutations.get(context);

		if (operations == null) {
			operations = new ObjectManagerWrite(context, new BatchMutation());
			contextMutations.put(context, operations);
		}

		return operations;
	}

	/**
	 * Get the mutations for this execution context
	 * 
	 * @param context
	 * @return
	 */
	private ObjectManagerDelete getDeletions(ObjectManager context) {
		ObjectManagerDelete operations = contextDeletions.get(context);

		if (operations == null) {
			operations = new ObjectManagerDelete(context, new BatchMutation());
			contextDeletions.put(context, operations);
		}

		return operations;
	}

}
